package com.qf.controller;

import com.qf.bean.User;
import com.qf.service.UserService;
import com.qf.utils.PageInfo;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * IntelliJ IDEA -> member
 * 2019/12/3 14:27
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/getUsers")
    public String getUsers(@RequestParam(defaultValue = "1") Integer index , String name ,ModelMap map){
        Page<User> userPage = userService.getUsers(index, PageInfo.PAGESIZE,name);
        for (User user : userPage.getContent()) {
            System.out.println("***"+user.getStatus());
        }
        map.addAttribute("userPage",userPage);
        map.addAttribute("name",name);
        return "userList";
    }

    @RequestMapping("/login")
    public void login(String name , String password, HttpServletResponse  response, HttpSession session)
    {
        try {
            User user = userService.login(name);
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            if (user == null ){
    out.println("<script>alert('该用户不存在');location.href='/login.jsp'</script>");
            }
          else {
                if (!user.getPassword().equals(password)) {
                    out.println("<script>alert('密码错误！');location.href='/login.jsp'</script>");
                }
                if (user.getStatus()==0){
                    out.println("<script>alert('该用户被冻结');location.href='/login.jsp'</script>");
                }
                else {
                    session.setAttribute("user",user);
                    out.println("<script>alert('登录成功！');location.href='/index.jsp'</script>");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
@RequestMapping("/loginout")
    public  String  loginout(HttpSession session){
        session.invalidate();
        return  "login";
}

@RequestMapping("/updatePassword")
    public void  updatePassword(String oldPassword,String newPassword ,HttpServletResponse response,HttpSession session){
    try {

        User user = (User) session.getAttribute("user");
        int len  = userService.updatePassword(user,oldPassword,newPassword);
        System.out.println("777"+len);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if (len > 0){
            out.println("<script>alert('修改成功！');location.href='/getUsers'</script>");
        }
        else if (len == -1){
            out.println("<script>alert('修改失败！原始密码不对');location.href='/updatePassword.jsp'</script>");
        }     else if (len == -2){
            out.println("<script>alert('修改失败！新密码和旧密码相等');location.href='/updatePassword.jsp'</script>");
        }  else {
            out.println("<script>alert('修改失败！');location.href='/updatePassword.jsp'</script>");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}
